package com.hand.demo.api.controller.v1;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.hand.demo.app.service.HhrTaEducationService;
import com.hand.demo.domain.entity.HhrTaEducation;
import com.hand.demo.domain.repository.HhrTaEducationRepository;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 标准化教育经历表(HhrTaEducation)表控制层
 *
 * @author
 * @since 2025-06-16 12:16:54
 */

@RestController("hhrTaEducationController.v1")
@RequestMapping("/v1/{organizationId}/hhr-ta-educations")
public class HhrTaEducationController extends BaseController {

    @Autowired
    private HhrTaEducationRepository hhrTaEducationRepository;

    @Autowired
    private HhrTaEducationService hhrTaEducationService;

    @ApiOperation(value = "标准化教育经历表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    public ResponseEntity<Page<HhrTaEducation>> list(HhrTaEducation hhrTaEducation, @PathVariable Long organizationId,
                                                     @ApiIgnore @SortDefault(value = HhrTaEducation.FIELD_ID,
                                                             direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<HhrTaEducation> list = hhrTaEducationService.selectList(pageRequest, hhrTaEducation);
        return Results.success(list);
    }

    @ApiOperation(value = "标准化教育经历表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{id}/detail")
    public ResponseEntity<HhrTaEducation> detail(@PathVariable Long id) {
        HhrTaEducation hhrTaEducation = hhrTaEducationRepository.selectByPrimary(id);
        return Results.success(hhrTaEducation);
    }

    @ApiOperation(value = "创建或更新标准化教育经历表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<List<HhrTaEducation>> save(@PathVariable Long organizationId, @RequestBody List<HhrTaEducation> hhrTaEducations) {
        validObject(hhrTaEducations);
        SecurityTokenHelper.validTokenIgnoreInsert(hhrTaEducations);
        hhrTaEducations.forEach(item -> item.setTenantId(organizationId));
        hhrTaEducationService.saveData(hhrTaEducations);
        return Results.success(hhrTaEducations);
    }

    @ApiOperation(value = "删除标准化教育经历表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<?> remove(@RequestBody List<HhrTaEducation> hhrTaEducations) {
        SecurityTokenHelper.validToken(hhrTaEducations);
        hhrTaEducationRepository.batchDeleteByPrimaryKey(hhrTaEducations);
        return Results.success();
    }

}

